package com.example.cofeed.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPUtils;
import com.example.basedialog.BaseDialog;
import com.example.basedialog.ExitDialog;
import com.example.cofeed.R;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.constant.SputilsConstant;
import com.example.cofeed.even.LoginEeven;
import com.example.cofeed.util.ToastUtils;
import com.google.gson.internal.$Gson$Preconditions;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import butterknife.OnClick;

;

public class SettingActivity extends BaseActivity {

    private TextView mTvBbh;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("设置");

        mTvBbh = findViewById(R.id.tv_bbh);

        mTvBbh.setText("版本号：" + AppUtils.getAppVersionName());
    }

    @SuppressLint("MissingPermission")
    @OnClick({R.id.tv_zt, R.id.tv_help, R.id.tv_feedback, R.id.tv_lxwm, R.id.tv_about_us, R.id.tv_login_out, R.id.tv_tssz, R.id.tv_xgmm
            , R.id.tv_yhxy})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.tv_zt:
                ActivityUtils.startActivity(SetFontActivity.class);
                break;
            case R.id.tv_help:
                intent = new Intent(this, WebViewActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, 1);
                startActivity(intent);
                break;
            case R.id.tv_feedback:
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, 7);
                startActivity(intent);
                break;
            case R.id.tv_lxwm:
//                PhoneUtils.call("110");
//                ApiService.service_phone(this, new DialogCallback<LzyResponse<Map<String, String>>>(this) {
//                    @Override
//                    public void onSuccess(Response<LzyResponse<Map<String, String>>> response) {
//                        String tel = response.body().data.get("tel");
//                        PhoneUtils.dial(tel);
//
//                    }
//                });
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, 8);
                startActivity(intent);
                break;
            case R.id.tv_about_us:
//                ActivityUtils.startActivity(AboutUsActivity.class);
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, 11);
                startActivity(intent);
                break;
            case R.id.tv_login_out:


                ExitDialog exitDialog = new ExitDialog();
                exitDialog.setOnclick(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        exitDialog.dismiss();
                        ApiService.logout(this, new DialogCallback<LzyResponse<Object>>((Activity) mContext) {
                            @Override
                            public void onSuccess(Response<LzyResponse<Object>> response) {
                                ApiService.sms_logout("sms_logout", new DialogCallback<LzyResponse<Object>>((Activity) mContext) {
                                    @Override
                                    public void onSuccess(Response<LzyResponse<Object>> response) {
                                        SPUtils.getInstance().put(SputilsConstant.TOKEN, "");
                                        SPUtils.getInstance().put(SputilsConstant.AUTO_TOKEN, "");
//                                                Intent intent = new Intent(mContext, LoginActivity.class);
//                                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                                                startActivity(intent);
                                        EventBus.getDefault().post(new LoginEeven());
                                        finish();
                                    }

                                    @Override
                                    public void onError(Response<LzyResponse<Object>> response) {
                                        super.onError(response);
                                        SPUtils.getInstance().put(SputilsConstant.TOKEN, "");
                                        SPUtils.getInstance().put(SputilsConstant.AUTO_TOKEN, "");
//                                                Intent intent = new Intent(mContext, LoginActivity.class);
//                                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                                                startActivity(intent);
                                        EventBus.getDefault().post(new LoginEeven());
                                        finish();
                                    }
                                });

                            }

                            @Override
                            public void onError(Response<LzyResponse<Object>> response) {
                                super.onError(response);
//                                        App.getInstance().exitApp();
                                SPUtils.getInstance().put(SputilsConstant.TOKEN, "");
                                SPUtils.getInstance().put(SputilsConstant.AUTO_TOKEN, "");
//                                        Intent intent = new Intent(mContext, LoginActivity.class);
//                                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                                        startActivity(intent);
                                EventBus.getDefault().post(new LoginEeven());
                                finish();

                            }
                        });

                    }
                });
                exitDialog.show(getSupportFragmentManager());

//                View inflate = View.inflate(mContext, R.layout.layout_exit, null);
//
//                AlertDialog dialog = new AlertDialog.Builder(this)
//                        .setView(inflate)
//                        .show();
//                inflate.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//
//                    }
//                });
//
//                inflate.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dialog.dismiss();
//                    }
//                });


//                        .setMessage("是否确定退出登录？")
//                    .setNegativeButton("取消", null)
//                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialogInterface, int which) {
//                            ApiService.logout(this, new DialogCallback<LzyResponse<Object>>((Activity) mContext) {
//                                @Override
//                                public void onSuccess(Response<LzyResponse<Object>> response) {
//                                    ApiService.sms_logout("sms_logout", new DialogCallback<LzyResponse<Object>>((Activity) mContext) {
//                                        @Override
//                                        public void onSuccess(Response<LzyResponse<Object>> response) {
//                                            SPUtils.getInstance().put(SputilsConstant.TOKEN, "");
//                                            SPUtils.getInstance().put(SputilsConstant.AUTO_TOKEN, "");
////                                                Intent intent = new Intent(mContext, LoginActivity.class);
////                                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
////                                                startActivity(intent);
//                                            EventBus.getDefault().post(new LoginEeven());
//                                            finish();
//                                        }
//
//                                        @Override
//                                        public void onError(Response<LzyResponse<Object>> response) {
//                                            super.onError(response);
//                                            SPUtils.getInstance().put(SputilsConstant.TOKEN, "");
//                                            SPUtils.getInstance().put(SputilsConstant.AUTO_TOKEN, "");
////                                                Intent intent = new Intent(mContext, LoginActivity.class);
////                                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
////                                                startActivity(intent);
//                                            EventBus.getDefault().post(new LoginEeven());
//                                            finish();
//                                        }
//                                    });
//
//                                }
//
//                                @Override
//                                public void onError(Response<LzyResponse<Object>> response) {
//                                    super.onError(response);
////                                        App.getInstance().exitApp();
//                                    SPUtils.getInstance().put(SputilsConstant.TOKEN, "");
//                                    SPUtils.getInstance().put(SputilsConstant.AUTO_TOKEN, "");
////                                        Intent intent = new Intent(mContext, LoginActivity.class);
////                                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
////                                        startActivity(intent);
//                                    EventBus.getDefault().post(new LoginEeven());
//                                    finish();
//
//                                }
//                            });
//
//                        }
//                    }).show();
                break;
            case R.id.tv_tssz:
                ActivityUtils.startActivity(PushSettingActivity.class);
                break;
            case R.id.tv_yhxy:
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(IntentKeyConstant.ID, -2);
                intent.putExtra(IntentKeyConstant.URL, "https://app.cofeed.com/treaty/treaty_detail");
                startActivity(intent);
                break;
            case R.id.tv_xgmm:

                ApiService.password_account_status(this, new DialogCallback<LzyResponse<Object>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<Object>> response) {
                        Map<String, String> map = (Map<String, String>) response.body().data;
                        String i = map.get("accountid");
                        int accountid = Integer.valueOf(i);

                        if (accountid > 0) {
                            Intent intent = new Intent(mContext, InputPhoneActivity.class);
                            intent.putExtra(IntentKeyConstant.TYPE, 1);
//                            intent.putExtra(IntentKeyConstant.ACCOUNT_ID, accountid);
                            startActivity(intent);
                            finish();

                        } else {
                            ToastUtils.showShort("您的手机没有绑定账号");
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<Object>> response) {
                        super.onError(response);
//                        if (response.body().code == 301){
//                            ToastUtils.showShort(response.body().message);
//                        }
                        Intent intent = new Intent(mContext, BindPhoneActivity.class);
//                        intent.putExtra(IntentKeyConstant.TYPE, 1);
                        startActivity(intent);
                    }
                });

//                intent = new Intent(mContext, InputPhoneActivity.class);
//                intent.putExtra(IntentKeyConstant.TYPE, 1);
//                startActivity(intent);
//                ActivityUtils.startActivity(BindPhoneActivity.class);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
